package com.zhaosc.mybatis.shared.table;

import java.util.List;

import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.defaults.RawSqlSource;
import org.apache.ibatis.session.Configuration;

import com.google.common.collect.Lists;



public class RawSqlSourceHandler implements ISqlSourceHandler {

	@Override
	public List<MySqlSource> handle(Configuration configuration, SqlSource sqlSource ,String mapperId, String sql, TableShared tableShared,
			Class<?> parameterType) {

		String tableName = tableShared.tableName();
		int tableNum = tableShared.tableNum();

		List<MySqlSource> mySqlSourceList = Lists.newArrayList();
		for (int i = 0; i < tableNum; i++) {
			String newSql = sql.replace(tableName, tableName + "_" + i);
			String myMapperId = mapperId.concat("-").concat(i + "");
			SqlSource sqlSourceNew = new RawSqlSource(configuration, newSql, parameterType);
			MySqlSource mySqlSource = MySqlSource.builder().sqlSource(sqlSourceNew).mapperId(myMapperId).build();
			mySqlSourceList.add(mySqlSource);
		}
		
		return mySqlSourceList;

	}

}
